﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Threading;
using System.Windows.Threading;
using CloudModels;
using CommonLibrary;
using Newtonsoft.Json;

namespace 云平台Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {

        }
        /// <summary>
        /// 获取传感器数据
        /// </summary>
        /// <param name="sensorid">传感器id</param>
        public string getData(string sensorid) {
            string results = "";
            HttpHelper http = new HttpHelper("http://172.16.11.63/");
            string token = Common.GetToken("http://172.16.11.63/", "user16", "123456", "wodiao");
             http.RequestData("GetNewestSensorData", "SensorData", "GetNewestSensorData?sensorid=" + sensorid, "AccessToken", token);
             string result= http.ResultMsg;
          
            if(!string.IsNullOrWhiteSpace(result)){
                 ResponseModel responmodel = JsonConvert.DeserializeObject<ResponseModel>(result);
                if(responmodel!=null){
                if(responmodel.Status== ResultStatus.Success){
                if(responmodel.AppendData!=null&&!string.IsNullOrWhiteSpace(responmodel.AppendData.ToString())){
                    SensorDataModel adm = JsonConvert.DeserializeObject<SensorDataModel>(responmodel.AppendData.ToString());
                    if(adm!=null){
                     results= adm.SensorData+" "+adm.SensorUnit;
                    }
                }
                }
                }
               
            }
            return results;

        }

        public bool setDevice(string actuatorId,bool isOpen) {
            bool isresultOpen = false;
            HttpHelper http = new HttpHelper("http://172.16.11.63/");
            string token = Common.GetToken("http://172.16.11.63/", "user16", "123456", "2");
            http.RequestData("OnOff", "Device", "OnOff?actuatorId=" + actuatorId + "&isOpen=" + isOpen, "AccessToken", token);
             string result= http.ResultMsg;
            if(!string.IsNullOrWhiteSpace(result)){
                ResponseModel responmodel = JsonConvert.DeserializeObject<ResponseModel>(result);
                if(responmodel!=null){
                if(responmodel.Status== ResultStatus.Success){
                    isresultOpen = true;
                }
                }
            }
            return isresultOpen;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("");
        }
        int index = 0;
        private void btn_light01_Click(object sender, RoutedEventArgs e)
        {
            if (index == 0)
            {
                setDevice("1", true);
                index = 1;
            }
            else {
                setDevice("1", false);
                index = 0;
            }
            
        }
        AbandonedMutexException EXCEPTION = new AbandonedMutexException();
        private void btn_light02_Click(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0,0,3);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            Thread thread = new Thread(getSeries);
            thread.IsBackground = true;
            thread.Start();
        }

        public void getSeries() {
            this.Dispatcher.Invoke(() => {
                lbl_feng.Content = getData("137");
            });
        }
    }
}
